local Msg = require("core.Msg")
local CopyFastExcel = require("excel.ComplexSrvActivity").copyFast
local Grid = require("bag.Grid")
local ActDefine = require("activity.Define")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local MailLogicFL = require("mail.MailLogicFL")
local CowryBagLogic = require("fuli.CowryBagLogic")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local HtmlUtil = require("common.HtmlUtil")
local Log = require("common.Log")
local CopyCommon = require("copy.CopyCommon")
local ComplexSrvActFL = require("activity.ComplexSrvActFL")

function open(human, id, type)
  local sendMsg = Msg.gc.GC_OPEN_COMPLEX_PAYDRAW_ACTIVITY
  local activityNum = ComplexSrvActFL.onGetCurActRuleID(id)
  local activity = CopyFastExcel[activityNum]
  if not activity then
    return
  end
  sendMsg.info[0] = #activity.condition
  local cache = {}
  for i=1, #activity.condition do
    if i==1 then
      sendMsg.info[i].reward[0] = 1
      Grid.create(cache, activity.reward[i][1], activity.reward[i][2])
      Grid.makeItem(cache, sendMsg.info[i].reward[1], 0, human)
    else
      sendMsg.info[i].reward[0] = 0
    end
    sendMsg.info[i].args[0] = 3
    sendMsg.info[i].args[1] = activity.images[i]
    sendMsg.info[i].args[2] = CopyCommon.getCopyEnterCount(human, activity.condition[i][1])
    sendMsg.info[i].args[3] = activity.condition[i][2]
    sendMsg.info[i].canGet = human.db.YYCopyFastIsGet
    sendMsg.info[i].name = ""
  end
  sendMsg.rewardInfo[0] = 0
  sendMsg.myInfo[0] = 0
  sendMsg.activityId = id
  sendMsg.type = type or 1
  Msg.send(sendMsg, human.fd)
end

--领取奖励
function getReward(human, id, index)  
  if human.db.YYCopyFastIsGet ~= ActDefine.REWARD_CAN_GET then
    return
  end
  local activityNum = ComplexSrvActFL.onGetCurActRuleID(id)
  local activity = CopyFastExcel[activityNum]
  if not activity then
    return
  end
  if not BagLogic.checkCanAddItemList(human, activity.reward, 1) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_BAG_NOT_ENOUGH)
  end
  BagLogic.addItem(human, activity.reward[1][1], 1, activity.reward[1][2], "complexactholidaygift")
  
  local str = string.format(Lang.YUNYING_CRIT_COPY_FAST, HtmlUtil.fontName(human.name, human._id), human._id)
  Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
  human.db.YYCopyFastIsGet = ActDefine.REWARD_ALREADY_GET
  open(human, id)
end

function checkCanGet(human, copyID)
  local activityNum = ComplexSrvActFL.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_COPYFAST)
  local activity = CopyFastExcel[activityNum]
  if not activity then
    return
  end
  if human.db.YYCopyFastIsGet ~= ActDefine.REWARD_NOT_CAN_GET then
    return
  end
  for i=1, #activity.condition do    
    if CopyCommon.getCopyEnterCount(human, activity.condition[i][1]) < activity.condition[i][2] then
      return
    end    
  end
  human.db.YYCopyFastIsGet = ActDefine.REWARD_CAN_GET
  open(human, ActDefine.OPERATEACT_TYPE_COPYFAST, 1)
end